/*
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2012 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package au.gov.aims.colouriseimage.servlet;

import au.gov.aims.colouriseimage.Colouriser;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ColouriseImage extends HttpServlet {
	private static final Logger LOGGER = Logger.getLogger(ColouriseImage.class.getName());

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.performTask(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.performTask(request, response);
	}

	private void performTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String imgUrlStr = request.getParameter("url");
		String colour = request.getParameter("c");

		int red = 255, green = 255, blue = 255;
		if (colour != null && !colour.isEmpty()) {
			if (colour.startsWith("#")) {
				colour = colour.substring(1);
			}

			if (colour.length() >= 6) {
				red = Integer.parseInt(colour.substring(0, 2), 16);
				green = Integer.parseInt(colour.substring(2, 4), 16);
				blue = Integer.parseInt(colour.substring(4, 6), 16);
			}
		}

		if (imgUrlStr != null && !imgUrlStr.isEmpty()) {
			try {
				URL url = Utils.toURL(imgUrlStr);
				if (url != null) {
					BufferedImage source = Colouriser.getBufferedImage(url);
					if (source == null) {
						LOGGER.log(Level.SEVERE, "Unsupported image file: {0}", imgUrlStr);
					} else {
						BufferedImage colourisedImage = Colouriser.colourImage(source, red, green, blue, imgUrlStr);

						// Always output as PNG, whatever the input, to prevent quantisation.
						response.setContentType("image/png");
						OutputStream out = response.getOutputStream();
						ImageIO.write(colourisedImage, "png", out);
					}
				}
			} catch(Exception ex) {
				LOGGER.log(Level.SEVERE, "Error", ex);
			}
		}


/*
		// TODO Remove this!! This snippet is used to manually test with local files...
		String fileStr = request.getParameter("file");
		if (fileStr != null && !fileStr.isEmpty()) {
			try {
				File file = new File(fileStr);
				if (file != null) {
					BufferedImage source = Colouriser.getBufferedImage(new FileInputStream(file));
					if (source == null) {
						LOGGER.log(Level.SEVERE, "Unsupported image file: {0}", fileStr);
					} else {
						BufferedImage colourisedImage = Colouriser.colourImage(source, red, green, blue);

						// Output as PNG, whatever the input.
						response.setContentType("image/png");
						OutputStream out = response.getOutputStream();
						ImageIO.write(colourisedImage, "png", out);
					}
				}
			} catch(Exception ex) {
				LOGGER.log(Level.SEVERE, "Error", ex);
			}
		}
*/


	}
}
